﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using LIM.forms.uc_DanhMuc_LoaiChiTieu;
using LIM.controller;
using LIM.model;
using Telerik.WinControls.UI;

namespace LIM.forms
{
    public partial class radForm_DanhMuc_LoaiChiTieu : Telerik.WinControls.UI.RadForm
    {
        public radForm_DanhMuc_LoaiChiTieu()
        {
            InitializeComponent();
            uc_DanhMuc_LoaiChiTieu_ThongTinLoaiChiTieu uc_DanhMuc_LoaiChiTieu_ThongTinLoaiChiTieu = new uc_DanhMuc_LoaiChiTieu_ThongTinLoaiChiTieu();
            uc_DanhMuc_LoaiChiTieu_ThongTinLoaiChiTieu.Dock = DockStyle.Fill;
            this.radPageViewPage_ThongTinLoaiChiTieu.Controls.Add(uc_DanhMuc_LoaiChiTieu_ThongTinLoaiChiTieu);

            uc_DanhMuc_LoaiChiTieu_DanhSachChiTieu uc_DanhMuc_LoaiChiTieu_DanhSachChiTieu = new uc_DanhMuc_LoaiChiTieu_DanhSachChiTieu();
            uc_DanhMuc_LoaiChiTieu_DanhSachChiTieu.Dock = DockStyle.Fill;
            this.radPageViewPage_DanhSachChiTieu.Controls.Add(uc_DanhMuc_LoaiChiTieu_DanhSachChiTieu);
        }
        public DanhMucLoaiMauController Controller = new DanhMucLoaiMauController();

        public delegate void GridViewDoubleClick();
        public event GridViewDoubleClick OnRowChange;

        public tbl_DanhMuc_LoaiMau SelectedEntity { set; get; }
        private void radForm_DanhMuc_ChiTieu_Load(object sender, EventArgs e)
        {
            radGridView3.DataSource = Controller.LayDanhSach_LoaiMau();
            radGridView3.ClearSelection(); 
        }

        private void radGridView3_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (IsClickedOnRowContent((RadGridView)sender, e.Location))
            {
                SelectedEntity = (tbl_DanhMuc_LoaiMau)this.radGridView3.CurrentRow.DataBoundItem;
                if (OnRowChange != null)
                    OnRowChange();
            }
        }
        /// <summary>
        /// Detect click on rows
        /// </summary>
        /// <param name="radGridView"></param>
        /// <param name="location"></param>
        /// <returns></returns>
        private bool IsClickedOnRowContent(RadGridView radGridView, Point location)
        {
            int excludeHeight = 0;
            if (radGridView.ShowFilteringRow)
                excludeHeight += radGridView.TableElement.FilterRowHeight;
            if (radGridView.ShowGroupPanel)
                excludeHeight += radGridView.TableElement.GroupHeaderHeight;
            if (radGridView.ShowColumnHeaders)
                excludeHeight += radGridView.TableElement.TableHeaderHeight;
            if (excludeHeight < location.Y)
            {
                return true;
            }

            return false;
        }
        public void RefreshData()
        {
            Controller.Refresh_tbl_DanhMuc_LoaiMau();
            radForm_DanhMuc_ChiTieu_Load(new object(), new EventArgs());
        }

        private void radMenuItem2_Click(object sender, EventArgs e)
        {
            radGridView3.ClearSelection();
            SelectedEntity = new tbl_DanhMuc_LoaiMau();
            if (OnRowChange != null)
                OnRowChange();
        }

        private void radMenuItem_Refresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }
    }
}
